package cn.wolfcode.luowowo2.articleserver.service.imp;

import cn.wolfcode.luowowo2.article.domain.Strategy;
import cn.wolfcode.luowowo2.article.domain.StrategyContent;
import cn.wolfcode.luowowo2.article.domain.StrategyDetail;
import cn.wolfcode.luowowo2.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo2.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo2.article.service.IDestinationService;
import cn.wolfcode.luowowo2.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo2.article.service.IStrategyService;
import cn.wolfcode.luowowo2.article.service.IStrategyTagService;
import cn.wolfcode.luowowo2.article.vo.StrategyStatisVO;
import cn.wolfcode.luowowo2.common.query.StrategyDetailQueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by Albert on 2019/7/9.
 */
@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper mapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo<StrategyDetail> query(StrategyDetailQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyDetail> list = mapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail,String[] tags) {
        String content = strategyDetail.getStrategyContent().getContent();

        //生成概要
        if (content.length()>200) {
            strategyDetail.setSummary(content.substring(200));
        }else{
            strategyDetail.setSummary(content);
        }
        //创建时间
        strategyDetail.setCreateTime(new Date());

        //通过strategy获得dest,再判断是否是国外目的地
        Strategy strategy =strategyService.get(strategyDetail.getStrategy().getId());
        strategyDetail.setDest(strategy.getDest());
        Boolean abroadFlag = destinationService.isAbroad(strategy.getDest().getId());


        if (strategyDetail.getId()==null) {
            mapper.insert(strategyDetail);
            //存入content
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);

            //保存标签与关系
            strategyTagService.save(tags,strategyDetail.getId());
        }else {
            mapper.updateByPrimaryKey(strategyDetail);

            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.update(strategyContent);

            //先删除标签关系再保存
            strategyTagService.update(tags,strategyDetail.getId());
        }
    }

    @Override
    public StrategyDetail get(Long id) {
        StrategyDetail strategyDetail = mapper.selectByPrimaryKey(id);
        StrategyContent strategyContent = strategyContentMapper.get(id);
        strategyDetail.setStrategyContent(strategyContent);
        return strategyDetail;
    }

    @Override
    public void updateStateById(Byte state, Long id) {
        mapper.updateStateById(state,id);
    }

    @Override
    public List<StrategyDetail> getStrategyByDestId(Long id) {
        return mapper.selectStrategyByDestId(id);
    }

    @Override
    public List<StrategyDetail> getByViewNumTop3(Long id) {
        return mapper.selectByViewNumTop3(id);
    }

    @Override
    public List<StrategyDetail> list() {
        return mapper.selectAll();
    }

    @Override
    public void updateRedisVO(StrategyStatisVO vo) {
        mapper.updateRedisVO(vo);
    }

    @Override
    public List<StrategyDetail> getByStrateyId(Long strategyId) {
        return mapper.selectByStrateyId(strategyId);
    }

}
